package com.fadadianqi.lottery;

import com.fadadianqi.goods.model.Goods;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.logistics.inventory.model.Item;
import com.fadadianqi.logistics.inventory.service.InventoryService;
import com.fadadianqi.logistics.sell.model.Bill;
import com.fadadianqi.logistics.sell.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LotteryService {

    private static double MAX_SALES = 1000; // 销售额
    private static double MIN_RATE_OF_PROFIT = 0.03;

    /*
     * 可以参与抽奖
     */
    public boolean canDoLottery(Bill bill) {
        Double profit = billService.getProfit(bill);
        Double realPayables = bill.getRealPayables();
        if (// 销售额大于MAX_SALES
                realPayables >= MAX_SALES
                        // 并且利润率大于 MIN_RATE_OF_PROFIT
                        && (profit / realPayables) > MIN_RATE_OF_PROFIT) {
            return true;

        } else {
            return false;
        }
    }

    /**
     * 根据获取可供抽奖的商品
     * 获取抽奖奖品的规则
     * 成本小于利润的30%的商品
     *
     * @param profit
     * @return
     */
    public List<Goods> getGoods(Bill bill) {
        double profit = bill.getRealPayables() - bill.getCost();
        double maxCost = profit * 0.03;
        // 获取成本小于 maxCost 且有货的商品目录
        // Le = less or equal
        List<Item> items = inventoryService.findLeCost(maxCost);
        if (items == null) {
            return null;
        }
        List<Goods> goodsList = new ArrayList<Goods>();
        for (Item item : items) {
            goodsList.add(goodsService.getGoodsById(item.getGoodsId()));
        }
        return goodsList;
    }

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private BillService billService;

    @Autowired
    private InventoryService inventoryService;
}
